﻿using System;
using System.Collections.Generic;
using System.IO;
using Ro.Common.RoArgs;
using Ro.Common.RoInterface;
using Ro.Common.RoInterface.UserType;

namespace Ro.AssistFunction.Environment
{
    /// <summary>
    /// 接口实现
    /// 环境初始化事件
    /// 1.完成RoTestArgsInfo给定值
    /// </summary>
    public class RoEnvEvents : IRoEnvEvents
    {
        /// <summary>
        /// 检查追加参数是否正确
        /// </summary>
        /// <param name="args">追加参数</param>
        public bool EnvEvent(string[] args)
        {
            RoPubArgments.RoTestArgsInfo = new RoTestArgsInfo();
            List<string> argsList = new List<string>(args); //转换为list<string>

            bool reback = false;

            if (!argsList[0].ToLower().Contains("tcs") && !argsList[1].ToLower().Contains("uimaps"))
            {
                RoPubArgments.RoOutputToConsole.OutputConsole(RoLogStatus.Fatal, "首参数应该为TCS文件路径（全路径），请修改后重试");
                RoPubArgments.RoOutputToConsole.OutputConsole(RoLogStatus.Fatal, "次参数应该为TCI文件夹路径（全路径），请修改后重试");
            }
            else
            {
                //如果2个参值都存在, 则写入RoTestArgsInfo类中
                RoPubArgments.RoTestArgsInfo.TcsFilePath = argsList[0];
                RoPubArgments.RoTestArgsInfo.TciFolderPath = argsList[1];

                //验证tcs文件路径/tci文件夹是否存在
                if (!File.Exists(RoPubArgments.RoTestArgsInfo.TcsFilePath) || !Directory.Exists(RoPubArgments.RoTestArgsInfo.TciFolderPath))
                {
                    RoPubArgments.RoOutputToConsole.OutputConsole(RoLogStatus.Fatal, "Tcs文件/Tci文件夹不存在或缺失，请检查后重试");
                }
                else
                {
                    //提取后期追加的参数
                    argsList.RemoveAt(0);
                    argsList.RemoveAt(0);

                    //有额外参数
                    if (argsList.Count != 0)
                    {
                        //一个额外的参数是应有如下的格式 /property:参数Id,参数值
                        foreach (string sigextraargs in argsList)
                        {
                            string s = sigextraargs.Replace("/property:", "");
                            string[] splStrings = s.Split(',');
                            Property sigCommandProperty = new Property
                            {
                                Id = splStrings[0],
                                Value = splStrings[1]
                            };
                            //将通过command的额外参数追加进入List内
                            RoPubArgments.SystemProperties.Add(sigCommandProperty);
                        }
                        reback = true;
                    }

                    //检查和创建临时文件夹
                    string tcsname = Path.GetFileNameWithoutExtension(RoPubArgments.RoTestArgsInfo.TcsFilePath);
                    CreateTempLog(tcsname);
                }
            }

            return reback;
        }


        /// <summary>
        /// 检查/创建临时Log文件夹、文件
        /// </summary>
        /// <param name="tcsfilename">tcs文件名称</param>
        private void CreateTempLog(string tcsfilename)
        {
            // 创建存放LOG的路径
            string nowtime = DateTime.Now.ToString("yyyy-MM-dd");

            //创建存放log的文件夹
            RoPubArgments.TempLogInfo.FolderPath = "C:/Temp/" + nowtime + "/" + tcsfilename;
            if (!Directory.Exists(RoPubArgments.TempLogInfo.FolderPath))
            {
                Directory.CreateDirectory(RoPubArgments.TempLogInfo.FolderPath);
            }

            //创建存放log的文件夹
            RoPubArgments.TempLogInfo.ImageFolderPath = RoPubArgments.TempLogInfo.FolderPath + "/Image";
            if (!Directory.Exists(RoPubArgments.TempLogInfo.ImageFolderPath))
            {
                Directory.CreateDirectory(RoPubArgments.TempLogInfo.ImageFolderPath);
            }
        }
    }
}
